/**
 * @description UI 和 面板 基类
 * @author mirahs
 * @email 2892727493@qq.com
 */
export default class ViewBase {
    /** 资源 Bundle 名, 如果为空加载 resources 资源 */
    public bundleName: string
    /** 资源路径 */
    public skinPath: string
    /** 资源实例化后的对象 */
    public skin: cc.Node
    /** 是否缓存(缓存为 true 只隐藏 skin 而不销毁) */
    public cache: boolean = false

    protected _args: any[] = []


    // 获取类名(项目发布后类名是混淆的)
    public getClassName() {
        const [className] = this.skinPath.split('/').slice(-1)
        return className
    }


    // 初始化
    public init(args: any[]) {
        this._args = args

        this.onInit()
    }

    // 初始化完成
    public initDone() {
        const buttons = this.skin.getComponentsInChildren(cc.Button)

        for (const button of buttons) {
            if (!button.node.name.startsWith('Btn')) {
                continue
            }

            button.node.on('click', (_event: cc.Button) => {
                this.onButtonClick(button.node)
            })
        }

        this.onInitDone()
    }

    // 重置
    public reset(args: any[]) {
        this._args = args

        this.onReset()
    }

    // 开始显示
    public showing() {
        this.onShowing()
    }

    // 显示完成
    public showed() {
        this.onShowed()
    }

    // 更新 view
    public updateView(args: any[]) {
        this.onUpdateView(args)
    }

    // 开始隐藏
    public hiding() {
        this.onHiding()
    }

    // 隐藏完成
    public hided() {
        this.onHided()
    }

    // 销毁
    public destroy() {
        this.onDestroy()
        this.skin.destroy()
    }


    protected onInit() { }
    protected onInitDone() { }
    protected onReset() { }
    protected onButtonClick(_button: cc.Node) { }
    protected onShowing() { }
    protected onShowed() { }
    protected onUpdateView(_args: any[]) { }
    protected onHiding() { }
    protected onHided() { }
    protected onDestroy() { }
}
